USE [FootballPoll]
GO

/****** Object:  StoredProcedure [dbo].[GetCurrentVotesByUserName]    Script Date: 12/02/2011 13:28:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetCurrentVotesByUserName]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetCurrentVotesByUserName]
GO

USE [FootballPoll]
GO

/****** Object:  StoredProcedure [dbo].[GetCurrentVotesByUserName]    Script Date: 12/02/2011 13:28:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[GetCurrentVotesByUserName] 
     /****** Pass them in as parameters  ******/
   
		@StartDate DATETIME,
		@EndDate DATETIME,
		@UserName varchar(255)
AS 
     /****** Explicit declaration for testing  ******/
    /*
		DECLARE @StartDate DATETIME
		DECLARE @EndDate DATETIME
		DECLARE @UserName varchar(255)


		SET @StartDate ='2011-10-23'
		SET @EndDate ='2011-10-29'
		SET @UserName = 'gonzo'
	*/		
	/****** Explicit declaration for testing  ******/

	

SELECT t.ID, t.TeamName, 
v.Points, v.FirstPlaceVotes, v.Date

FROM Teams t

	LEFT JOIN Votes v
	ON v.TeamID = t.ID		
	AND v.Date BETWEEN @StartDate AND @EndDate	 	
	
	LEFT JOIN Users u
	ON u.ID = v.UserID
	AND u.UserName =@UserName

WHERE v.Points IS NOT NULL
ORDER BY v.Points DESC, t.TeamName



GO

